<?php

namespace App\Admin\Controllers;
use Illuminate\Support\MessageBag;
use App\crm_hetong;
use App\crm_hetongtype;
use App\crm_deal;
use App\crm_order;
use App\crm_customer;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_hexiaoController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('合同核销管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('合同核销管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('合同核销管理');
            $content->description('添加');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_hetong::class, function (Grid $grid) {

            $grid->model()->where('statue', 3)->orwhere("statue",4);
            $grid->id('序号')->sortable();
            $grid->type('合同类型')->display(function () {
                $list=crm_hetongtype::all()->pluck('title','id');
                return $list[$this->type];
            });
            $grid->hetong('合同号');
            $grid->deal_id('线路名称')->display(function ($deal_id) {
                return crm_order::find(crm_deal::find($deal_id)->deal_orderid)->order_xianlu;
            });
            $grid->customer_id('客户姓名')->display(function ($customer_id) {
                return crm_customer::find($customer_id)->customer_name;
            });
            $grid->column('收款金额')->display(function () {
                return crm_deal::find($this->deal_id)->deal_sshoukuan;
            });
            $grid->column('请款金额')->display(function () {
                return crm_deal::find($this->deal_id)->deal_sdakuan;
            });
            $grid->column('实际毛利')->display(function () {
                return crm_deal::find($this->deal_id)->deal_lirun;
            });
            $grid->users_id('领用人');
            $grid->statue('状态')->display(function ($name) {
                $list=['1'=>'入库','2'=>'领用','3'=>'待核销','4'=>'已核销'];
                return $list[$this->statue];
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $list=crm_hetongtype::all()->pluck('title','id');
                // $filter->in('customer_id', '客户姓名')->multipleSelect(['key' => 'value']);
                $filter->equal('type', '合同类型')->select($list);
                $filter->like('hetong', '合同号');
                $filter->like('users_id', '领用人');
            });
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append('<a href="'.admin_url('crm_order').'/'.crm_deal::find($actions->row->deal_id)->deal_orderid.'/edit'.'" class="btn btn-sm btn-primary">查看 </a>      ');
                if($actions->row->statue!=4){
                    $actions->append('<a href="'.admin_url('crm_hexiao').'/zhuangtai/'.$actions->getKey().'" class="btn btn-sm btn-primary">核销</a>');
                }
            });
            $grid->disableExport();//禁用导出按钮
            $grid->disableCreation();//禁用新增
            $grid->disableRowSelector();//禁用行选择
        });
    }
    // 状态改变
    public function zhuangtai($id)
    {
        $hetong=crm_hetong::find($id);
        $hetong->statue=4;
        $res=$hetong->save();
        if($res){
            $success = new MessageBag([
                    'title'   => '核销成功',
            ]);
            return redirect(admin_url('crm_hexiao'))->with(compact('success'));
        }
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(crm_hetong::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
